package com.zhiyin.seal.exception;

import com.zhiyin.seal.constant.ResponseCodeConst;
import com.zhiyin.seal.entity.dto.ResponseDTO;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public ResponseDTO globalException(HttpServletRequest request, NullPointerException ex){

        return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
    }

    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public ResponseDTO globalException(HttpServletRequest request, BusinessException ex){

        return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
    }

    @ResponseBody
    @ExceptionHandler(ResponseCodeException.class)
    public ResponseDTO globalException(HttpServletRequest request, ResponseCodeException ex){

        return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
    }

    @ResponseBody
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseDTO globalException(HttpServletRequest request, DuplicateKeyException ex){

        return ResponseDTO.wrap(ResponseCodeConst.DUPLICATE_KEY_ERROR);
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseDTO globalException(HttpServletRequest request, Exception ex){

        return ResponseDTO.wrap(ResponseCodeConst.SYSTEM_ERROR);
    }
}
